import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/event/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/widget/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/entity/_.dart';

class CountryList extends StatefulWidget {
  final String countryIndex;
  CountryList({required this.countryIndex});
  @override
  _CountryList createState() => _CountryList(countryIndex: int.parse(countryIndex));
}

class _CountryList extends State<CountryList> {

  _CountryList({required int countryIndex}){
    this.focusCountry = countryIndex;
  }

  List<Continent> continentList =[];
  TextStyle textStyle = TextStyle(
      fontSize: Screen.setFontSize(18),
      fontWeight: FontWeight.w600,
      color: ColorConfig.BlackBackColor);
  late int focusCountry;
  Color bottomColor = ColorConfig.ThemeColor;

  @override
  void initState() {
    super.initState();
    getCountryList();
  }

  @override
  void dispose() {
    super.dispose();
  }

  Future<void> getCountryList()async{
    continentList =  (await CountryService.getAllCountry())!;
    setState(() {

    });
  }

  dynamic clickEditItem({required ClickModel clickModel,required int countryId}){
      switch(clickModel){
        case ClickModel.Focus :
          setState(() {
            focusCountry = countryId;
          });
          return;
        default :
          setState(() {
            focusCountry = 0;
          });
          return 0;
      }
  }

  Widget bottom() {
    return Container(
      height: Screen.setHeight(60),
      alignment: Alignment.center,
      child: GestureDetector(
        onTapDown: (TapDownDetails details){
          bottomColor = ColorConfig.ThemeObsColor;
          setState(() {

          });
        },
        onTapUp: (TapUpDetails details){
          bottomColor = ColorConfig.ThemeColor;
          setState(() {

          });
        },
        onTap: ()async{
          if(focusCountry ==0){
            BotToast.showText(text:'您还未筛选国家!');
            return;
          }
          await SPreferences().setInt('country_Index', focusCountry);
          Modular.to.pop();
           Config.eventBus.fire(ChangeCountryEvent(true,focusCountry));
          BotToast.showText(text:'筛选成功~');
        },
        child: Container(
          decoration: BoxDecoration(
            color: bottomColor,
            borderRadius: BorderRadius.all(Radius.circular(5))
          ),
          width: Screen.setWidth(280),
          height: Screen.setHeight(45),
          child: Center(
              child: Text(
                '确定',
                textAlign: TextAlign.center,
                style: TextStyle(
                    fontSize: Screen.setFontSize(18),
                    fontWeight: FontWeight.w600,
                    color: ColorConfig.WhiteBackColor
                ),
              )
          ),
        ),
      ),
    );
  }

  Column continent(Continent continent){

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          height: Screen.setHeight(10),
        ),
        Padding(
          padding: EdgeInsets.all(Screen.setHeight(20)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                continent.name!,
                style: textStyle,
              ),
              SizedBox(
                height: Screen.setHeight(15),
              ),
              Wrap(
                alignment: WrapAlignment.start,
                spacing: Screen.setHeight(15),
                runSpacing: Screen.setHeight(15),
                children: continent.countryList!
                    .map((e) => GestureDetector(
                  child: CountryEditItem(data: e,isFocus:e.id == focusCountry,callBack: clickEditItem,),
                ))
                    .toList(),
              )
            ],
          ),
        )
      ],
    );

  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: ColorConfig.WhiteBackColor,
          elevation: 2,
          leading: IconButton(
            icon: Icon(
              Icons.clear,
              size: Screen.setFontSize(20),
            ),
            onPressed: () {
              Modular.to.pop();
            },
            color: ColorConfig.BlackBackColor,
          ),
          title: Text(
            '筛选',
            style: textStyle,
          ),
          centerTitle: true,
        ),
        body: ListView(
          physics: BouncingScrollPhysics(),
          shrinkWrap: true,
          children: continentList.map((e) => continent(e)).toList(),
        ),
        bottomNavigationBar: bottom(),
      ),
      onWillPop: () async{
        Modular.to.pop();
        return true;
      },
    );
  }
}
